package com.uniquesoft.common;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

public class CommonUtils {

    public static int getMax(short[] arr) {  
    	  
        int max = arr[0];  
      
        for (int x = 1; x < arr.length; x++) {  
            if (arr[x] > max)  
                max = arr[x];  
      
        }  
        return max;  
    }

    public static int getMin(short[] arr) {  
    	  
        int min = arr[0];  
      
        for (int x = 1; x < arr.length; x++) {  
            if (arr[x] < min)  
                min = arr[x];  
      
        }  
        return min;  
    }
    
    public static boolean checkNull(Object obj) {
        if ((obj != null) && (obj.toString().trim().length() > 0))
            return false;
        else
            return true;
    }
    
    public static boolean isMapBlank(Object obj) {
    	String str = obj.toString().replaceAll("(\\s*)", "");
    	str = str.replaceAll("\\{\\}", "");
        if ((obj != null) && (str.length() > 0))
            return false;
        else
            return true;
    }
    
    public static Object StringToDate(String str) throws ParseException {
    	Date date = null;
    	String strFt = str.trim();
    	if(str.indexOf('(')>=0){
    		strFt = str.substring(0,str.indexOf('(')).trim();
    	}
    	
    	final SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
    	final SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM");
    	String regex = "([0-9]{4}-[0-9][0-9]?(-[0-9]{2})?)|([0-9]{4}/[0-9][0-9]?(/[0-9]{2})?)|([0-9]{4}\\.[0-9][0-9]?(\\.[0-9]{2})?)"; 
    	
    	if(Pattern.compile(regex).matcher(strFt).matches()){
    		strFt = strFt.replace('/', '-');
    		strFt = strFt.replace('.', '-');
    		if (strFt.split("-").length == 3) {
        		date = dateFormat1.parse(strFt);
    		} else {
        		date = dateFormat2.parse(strFt);
    		}
    		return date;
    	}  else {
    		return str;
    	}
    }
    
}
